﻿/**********************************************************
 * 
 * 功能：用于数据的读取
 * 
 * 
 * */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using LitJson;

public class DataLoad{

    private PlayerInfo _playerInfo;
    private IPaddress _ipAddress;

    private const string playerInfoPath = "PlayerInfo";
    private const string ipAddressPath = "IpConfig";

    public PlayerInfo playerInfo
    {
        get
        {
            return _playerInfo;
        }
    }

    public IPaddress ipAddress
    {
        get
        {
            return _ipAddress;
        }
    }

    public DataLoad()
    {
        _playerInfo = new PlayerInfo();
        _ipAddress = new IPaddress();

        loadPlayerInfo(playerInfoPath);
        loadIP(ipAddressPath);
    }

    //加载玩家信息
    private void loadPlayerInfo(string path)
    {

        //string filePath = Application.dataPath + "/Resources/Configs/" + path + ".json";

        //if (File.Exists(filePath))
        //{


        //    StreamReader sr = new StreamReader(filePath);
        //    string str = sr.ReadToEnd();
        //    sr.Close();

        //    _playerInfo = JsonMapper.ToObject<PlayerInfo>(str);

        //    Debug.Log("PlayerInfo，配置文件加载成功");
        //}
        //else
        //{

        //    Debug.Log("配置文件路径错误");
        //}
        string filePath = "Configs/" + path;
        string tmp = Resources.Load<TextAsset>(filePath).text; 
        _playerInfo = JsonMapper.ToObject<PlayerInfo>(tmp);
        
    }
    //加载ip地址和端口号
    private void loadIP(string path)
    {
        //string filePath = Application.dataPath + "/Resources/Configs/" + path + ".json";

        //if (File.Exists(filePath))
        //{


        //    StreamReader sr = new StreamReader(filePath);
        //    string str = sr.ReadToEnd();
        //    sr.Close();

        //    _ipAddress = JsonMapper.ToObject<IPaddress>(str);

        //    Debug.Log("IpAdress，配置文件加载成功");
        //}
        //else
        //{

        //    Debug.Log("配置文件路径错误");
        //}
        string filePath = "Configs/" + path;
        string tmp = Resources.Load<TextAsset>(filePath).text; ;
        _ipAddress = JsonMapper.ToObject<IPaddress>(tmp);
    }
	
}

public class PlayerInfo
{
    public string name = null;
}

public class IPaddress
{
    public string ip = null;
    public string port = null;
}
